/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import Controller.StarClusterController;
import Model.DataSet;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JTable;

/**
 *
 * @author Snowangelic
 */
public class DataSetTable extends JTable implements MouseListener {
    DataSet dataSet;
    StarClusterMainWindow main;
    StarClusterController dataController;

    public DataSetTable(){
        addMouseListener(this);
        //DataSetTableModel tableModel=new DataSetTableModel();
        //this.setModel(tableModel);
    }

    public void setDataSet(DataSet dataSet){
        this.dataSet=dataSet;
        DataSetTableModel tableModel=new DataSetTableModel();
        tableModel.setController(dataController);
        tableModel.setDataSet(dataSet);
        this.setModel(tableModel);
    }

    public void setParent(StarClusterMainWindow mainWin){
        main=mainWin;
    }

    void attributeAdded(){
        /*
        int attributeIndex=dataSet.getAttributeNbr()-1;
        TableColumn newColumn=new TableColumn();
        newColumn.setHeaderValue(dataSet.getAttributeName(attributeIndex));
        */
        repaint();
    }

    void setController(StarClusterController controller) {
        this.dataController=controller;
    }

    void update(){
       
        DataSetTableModel tableModel=new DataSetTableModel();
        tableModel.setController(dataController);
        tableModel.setDataSet(dataSet);
        this.setModel(tableModel);

        if(this.getColumnCount()>=2){
            this.getColumnModel().getColumn(0).setPreferredWidth(30);
            this.getColumnModel().getColumn(0).setWidth(40);
            this.getColumnModel().getColumn(0).setMaxWidth(40);
            this.getColumnModel().getColumn(0).setMinWidth(40);

            this.getColumnModel().getColumn(0).setResizable(false);
            this.setColumnSelectionAllowed(false);
            this.setRowSelectionAllowed(true);
            this.getTableHeader().setReorderingAllowed(false);
        }
        /*
            //--- On initialise le Header ---
            for(int i=0;i<dataSet.getAttributeNbr();i++){
              TableColumn newColumn=new TableColumn();
              newColumn.setHeaderValue(dataSet.getAttributeName(i));
              this.getColumnModel().addColumn(newColumn);
            }
            
            /*
            //--- On ajoute les actions ---
            for(int i=0;i<dataSet.getActionNbr();i++)
                for(int j=0;j<dataSet.getAttributeNbr();j++){
                    this.setValueAt(dataSet.getValue(i,j), i, j);
                }
        
             * */
        
        repaint();
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        updateActionSelection();
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateActionSelection(){
        main.updateSelectedActions(this.getSelectedRows());
    }
}
